<?php

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Model;

class Company extends Model
{

    use SoftDeletes;

    const STATUS = [//状态
        0 => '禁用',
        1 => '正常',
    ];

    protected $table = 'company';


    public static function selectOption()
    {
        return self::query()->orderBy('id')->pluck('name', 'id');
    }

    public function province_info()
    {
        return $this->hasOne(Region::class, 'province_id', 'id');
    }

    public function city_info()
    {
        return $this->hasOne(Region::class, 'city_id', 'id');
    }



    public static function boot()
    {
        parent::boot();

        static::created(function ($model) {
            //添加默认资费规则
            $service_type_list = ServiceType::query()->get();
            $default_tariff = [];

            foreach ($service_type_list as $key => $service_type) {

                $default_tariff[] = [ //乘客
                    'company_id' => $model->id,
                    'type' => 1,
                    'service_type_id' => $service_type->id,
                    'name' => '默认规则',
                    'open_time'=> date('Y-m-d', time()),
                    'basics' => json_encode([
                        ['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 6],
                    ]),
                    'mileage' => json_encode([
                        ['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 1.5],
                        ['start_time'=> '00:00', 'end_time'=> '05:00', 'price' => 2.2],
                        ['start_time'=> '23:00', 'end_time'=> '00:00', 'price' => 2.2],
                    ]),
                    'duration' => json_encode([
                        'data'=> [['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 0.3]], 'type'=> 1
                    ]),
                    'distance' => json_encode([
                        ['start_dist'=> 8, 'price'=> 0.7, 'description'=>'超出8公里后，加收远途费']
                    ]),
                    'waiting_timeout'=> json_encode([
                        'data'=> [['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 0.3]],
                        'type'=> 1, 'start_minute'=> 3
                    ]),
                    'other_settings' => json_encode(['anticipation' => 0, 'accountability_cancel_time'=> 3, 'accountability_cancel_price'=> 3]),
                    'creator_id'=> 0
                ];

                $default_tariff[] = [ //司机
                    'company_id' => $model->id,
                    'type' => 2,
                    'service_type_id' => $service_type->id,
                    'name' => '默认规则',
                    'open_time'=> date('Y-m-d', time()),
                    'basics' => json_encode([
                        ['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 4.2],
                    ]),
                    'mileage' => json_encode([
                        ['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 1],
                        ['start_time'=> '00:00', 'end_time'=> '05:00', 'price' => 1.5],
                        ['start_time'=> '23:00', 'end_time'=> '00:00', 'price' => 1.5],
                    ]),
                    'duration' => json_encode([
                        'data'=> [['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 0.2]]
                    ]),
                    'distance' => json_encode([
                        ['start_dist'=> 8, 'price'=> 0.5, 'description'=>'超出8公里后，加收远途费']
                    ]),
                    'waiting_timeout'=> json_encode([
                        'data'=> [['start_time'=> '普通时段', 'end_time'=> '普通时段', 'price' => 0.3]],
                        'type'=> 1, 'start_minute'=> 3
                    ]),
                    'other_settings' => json_encode(['settle_day' => 7, 'accountability_cancel_time'=> 3, 'accountability_cancel_price'=> 3]),
                    'creator_id'=> 0
                ];
            }
            Tariff::query()->insert($default_tariff);

        });
    }



    /**
     * 公司业务类型
     * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
     * @Author 姿势就是力量
     */
    public function service_type()
    {
        return $this->hasManyThrough(
            ServiceType::class,
            CompanyServiceType::class,
            'company_id',
            'id',
            'id',
            'type_id');
    }

}
